package com.dunqi.gpm.cangxi.develop.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseFragment;
import com.dunqi.gpm.cangxi.common.util.SwipeUtil;
import com.dunqi.gpm.cangxi.develop.adapter.EarlyWarningAdapter;
import com.dunqi.gpm.cangxi.develop.bean.EarlyWarning;
import com.dunqi.gpm.cangxi.home.activity.ProjectDetailActivity;
import com.dunqi.gpm.cangxi.home.bean.Project;
import com.dunqi.gpm.cangxi.home.constance.Constance;
import com.dunqi.gpm.cangxi.home.fragment.HomeFragment;
import com.dunqi.gpm.cangxi.home.widget.EmptyView;
import com.dunqi.gpm.cangxi.mine.util.UserUtil;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/12/10.
 */

public class EarlyWarningFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    ArrayList<EarlyWarning> data = new ArrayList<>();
    private EarlyWarningAdapter adapter;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    @SuppressLint("CheckResult")
    private void net() {
        ApiFactory.getInstance()
                .queryProjectAlarmList(UserUtil.getUDA(), page)
                .compose(RxSchedulers.io_main())
                .subscribe(pages -> {
//                    ArrayList<EarlyWarning> list = pages.getList();
                    //去除前期
                    ArrayList<EarlyWarning> list = new ArrayList<>();
                    for (EarlyWarning item : pages.getList()) {
                        if (item.getAlarmSourceType() != 1) {//不为前期
                            list.add(item);
                        }
                    }
                    SwipeUtil.loadCompleted(swipeRefresh);
                    if (page == 1) {
                        adapter.replaceData(list);
                    } else {
                        adapter.addData(list);
                    }
                    page++;
                    if (pages.isHasNextPage()) {
                        adapter.loadMoreComplete();
                    } else {
                        adapter.loadMoreEnd(true);
                    }
                }, throwable -> {
                    SwipeUtil.loadCompleted(swipeRefresh);
                    adapter.loadMoreFail();
                });
    }

    @Override
    protected void initUiAndListener() {

        recycler.setLayoutManager(new LinearLayoutManager(context));

        adapter = new EarlyWarningAdapter(data);
        recycler.setAdapter(adapter);

        swipeRefresh.setOnRefreshListener(this);

        adapter.setEmptyView(new EmptyView(context));
        adapter.setOnLoadMoreListener(this, recycler);

        adapter.setOnItemClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_early_warning;
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        EarlyWarning warning = data.get(position);
        HomeFragment.homeMenuId = Constance.MainMenu.MENU_EARLY_WARNING;

        Project project = new Project();
        project.setProjectId(warning.getProjectId());
        ProjectDetailActivity.start(context, project, 0);
    }
}
